<?php include("src/editUser_cs.php");
@session_start();
$_SESSION['varMenu'] = 'Usuarios'; 
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script src="../js/jquery.js" type="text/javascript"></script>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Editar Usuario</title>
<link href="style.css" rel="stylesheet" type="text/css" />

<script type="text/javascript">
<!--
function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}
function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
//-->
</script>

</head>

<body class="style" onload="MM_preloadImages('images/menu3.png','images/menu2.png','images/noter (1).png','images/assignr.png','images/new_taskr.png','images/mailr.png','images/edit_2r.png','images/client_3r.png','images/deleter.png','images/backr.png')">

<div id="container">
  <?php include('header.php');?>
  <div id="mainContent">  
    <?php include('menuTop.php');?>
    <div id="bg">
	<form name="form" id="form" method="post" action="" enctype="multipart/form-data">
	<input type="hidden" name="editar" id="editar" value="1"/>
	<input type="hidden" name="login_ant" id="login_ant" value="<?php echo $datos_editar->login; ?>"/>
	<input type="hidden" name="passAnt" id="passAnt" value="<?php echo $datos_editar->password;?>" />
      <table width="100%" border="0" cellspacing="0" cellpadding="4">
        
        <tr>
          <td><H1>Editar Usuario</H1></td>
        </tr>
        <tr>
          <td align="center"><table width="80%" border="0" cellpadding="8" cellspacing="0" bgcolor="#FFFFFF">
            <tr>
              <td colspan="2" align="center" valign="top" bgcolor="#400000">&nbsp;</td>
              </tr>
            <tr>
              <td width="75%" align="center" valign="top">
                
               <table width="70%" border="0" align="center" background="images/table1.jpg">
				<tr>
					<td width="32%"><span class="Estilo3"> NOMBRE:</span> </td>
					<td width="3%">&nbsp;</td>
					<td width="65%">
					<input type="text" name="name" id="name" size="40" 
					value="<?php if(isset($datos_editar)) echo $datos_editar->name?>"/> 
					</td>
				</tr>
                <tr>
					<td width="32%"><span class="Estilo3"> DEPENDENCIA:</span> </td>
					<td width="3%">&nbsp;</td>
					<td width="65%">
					<input type="text" name="dependencia" id="dependencia" size="40" 
					value="<?php if(isset($datos_editar)) echo $datos_editar->dependencia?>"/> 
					</td>
				</tr>
				<tr>
					<td><span class="Estilo3">EMAIL:</span></td>
					<td>&nbsp;</td>
					<td>
					<input type="text" name="email" id="email" size="40" 
					value="<?php if(isset($datos_editar)) echo $datos_editar->email?>"/>
					</td>
				</tr>
				<tr>
					<td><span class="Estilo3">TELÉFONO:</span></td>
					<td>&nbsp;</td>
					<td>
					<input type="text" name="phone" id="phone" size="40" 
					value="<?php if(isset($datos_editar)) echo $datos_editar->phone?>"/></td>
				</tr>
				<tr>
					<td>USUARIO:</td>
					<td>&nbsp;</td>
					<td>
					<input type="text" name="login" id="login" size="40" 
					value="<?php if(isset($datos_editar)) echo $datos_editar->login?>"/>
					</td>
				</tr>  
				<tr>
					<td>CONTRASEÑA:</td>
					<td>&nbsp;</td>
					<td>
					<input type="password" name="password" id="password" size="40" 
					value=""/>
					</td>
				</tr>
				<tr>
					<td>CONFIRMAR CONTRASEÑA:</td>
					<td>&nbsp;</td>
					<td>
					<input type="password" name="confirmar" id="confirmar" size="40" 
					value=""/>
					</td>
				</tr>
				<?php 
				if(isset($datos_editar) && $datos_editar->code!='1')
				{
				?>
				<tr>
					<td>TIPO USUARIO: </td>
					<td>&nbsp;</td>
					<td>
						<table width="100%" border="0">
							<tr>
								<td>
									<select name="type" id="type" >
										<?php
										$i=0;
										while($i<count($user_types))
										{			
										?>
										<option value="<?php echo $user_types[$i];?>" <?php if($datos_editar->type==$user_types[$i]) echo 'selected="selected"';?> ><?php echo $user_types[$i];?></option>																		
	                              		<?php
											$i++;
										}
										?>
								  </select>
								</td>
							</tr>
					</table>
					</td>
				</tr>			
				<tr>
					<td>ESTADO USUARIO:</td>
					<td>&nbsp;</td>
					<td>
						<table width="100%" border="0">
							<tr>
								<td style="text-decoration:none; color:#008000">
								<input type="radio" name="status" value="1"  
								<?php 
								if(isset($datos_editar) && $datos_editar->status=='1') 
								echo 'checked'?>/>
								Activo
								</td>
								<td style="text-decoration:none; color:#FF6600">
								<input type="radio" name="status" value="2"  
								<?php 
								if(isset($datos_editar) && $datos_editar->status=='2') 
								echo 'checked'?>/>
								Inactivo
								</td>
								<td style="text-decoration:none; color:#0000CD">
								<input type="radio" name="status" value="3"  
								<?php 
								if(isset($datos_editar) && $datos_editar->status=='3') 
								echo 'checked'?>/>
								Bloqueado
								</td>
								<?php 
								if(isset($datos_editar) && $datos_editar->status=='4') 
								{
								?>
								<td style="text-decoration:none; color:#FF0000">
								<input type="radio" name="status" value="4"  checked="checked"/>
								Deleted
								</td>
								<?php 
								}
								?>
							</tr>
						</table>
					</td>
				</tr>
				
				<?php
				}
				?>
				<tr id="image">
					<td><span class="Estilo3"> <span id="result_box"><span title="">IMAGEN ACTUAL</span></span>:&nbsp;</span></td>
					<td>&nbsp;</td>
					<td >
					<div align="left">
					<img src="<?php if(isset($datos_editar->renam)&& $datos_editar->renam!='')
					{ echo 'file_upload/documentos/'.$datos_editar->renam; }
					else{ echo 'images/client.png';}?>" width="50" height="50" border="0"/>
					
					<?php if(isset($datos_editar->renam)&& $datos_editar->renam!='')
					{
					?>
					<a href="javascript:;" style="text-decoration:none;" 
					onclick="javascript: return imageDelete(<?php echo $datos_editar->code;?>);" 
					title="Delete Image">
					<img src="../images/delet.png" border="0" alt="Image not loaded" width="20" height="20"/></a>
					<?php
					}
					?>
					</div>
					</td>
				</tr>
				<tr>
					<td><span class="Estilo3"> CAMBIAR IMAGEN:&nbsp;</span></td>
					<td>&nbsp;</td>
					<td><input type="file" name="archivo[]" /></td>
				</tr>
				<tr>
					<td colspan="3">&nbsp;</td>
				</tr>
				<tr>		
					<td colspan="3" align="center" > 
						<input type="button" value="Guardar"  onclick="javascript: return checkLogin();"/>
						&nbsp;&nbsp;
						<input onClick="javascript:window.location.href='list_user.php';" type="button" name="Submit" value="Cancelar" />	
					</td>
				</tr>
			</table>
                </td>
              
            </tr>
            <tr>
              <td colspan="2" align="center" valign="top" bgcolor="#400000"><br /></td>
              </tr>
          </table>
          <br /></td>
        </tr>
      </table>
	</form>  
    </div>
    <br />
    <br />
    <!-- end #mainContent -->
  </div>
  <?php include('footer.php');?>
<!-- end #container --></div>

</body>
</html>
<script language="javascript" type="text/javascript">
	
	function checkLogin()
	 {
	 	var login  			= document.getElementById("login").value;
	 	var loginAnterior 	= document.getElementById("login_ant").value;
		var pass	 		= document.getElementById("password").value;
		var conf	 		= document.getElementById("confirmar").value;
		
			if(login != loginAnterior )
			{
				if(pass != '')
				{
					if(pass == conf)
					{
						jQuery.ajax({
						type: "POST",
						url: "userDuplicatecheck.php",
						data: 'login='+login,
						success: function(msg)
						{						
							if(msg==0)
							{	
								validate();				
							}
							else
							{
								alert('The login is not available');			
								document.getElementById("login").value = ""				
							}			
						}
						});
					}
					else
					{
						alert('Passwords must match');
						return false;
					}
				}
				else
				{
					alert('Please enter your Password');
					return false;
				}
		  }
		  else
		  {
			validate()
		  }	
		
		 	
	 }
	 
	function imageDelete(code)
	{
		   jQuery.ajax({
				type: "POST",
				url: "daleteImageUser.php",
				data: 'code='+code,
				success: function(msg)
				{			
					if(msg==1)
					{	
						tr = document.getElementById("image");
						padre = tr.parentNode;
						padre.removeChild(tr);								
					}
					else
					{
						alert('The image could not be removed');	
					}			
				}
				});				
	}
	
	function validate()
	{ 
	  var msg    = '';
	 
	  var name   = document.getElementById("name").value;
	  var email  = document.getElementById("email").value;
	  var phone  = document.getElementById("phone").value;
	  var login  = document.getElementById("login").value;
	  var p1     = document.getElementById("password").value;
      var p2     = document.getElementById("confirmar").value;     
	  //var type   = document.getElementById("type").value;     
	
	  if(name == '') 
	  msg='Please enter your name. \n';
	 
	  if(email == '') 
	  msg= msg+'Please enter your email. \n';	  
	  
	  if(phone == '') 
	  msg= msg+'Please enter your phone. \n';
	  
	  if(login == '') 
	  msg= msg+'Please enter your login.\n';	 
	  
	  //if(type == 'seleccione') 
	  //msg= msg+'Please select your user type.\n';	 
	 	
	  var espacios = false;
	  var cont = 0;
		 
	  if(p1 != '')
	  {
		  while (!espacios && (cont < p1.length))
		  {
			if (p1.charAt(cont) == " ")
			  espacios = true;
			  cont++;
		  }
		
		  if (espacios)
		  {
			 msg= msg+'The password can not contain spaces. \n';	 
		  }
		 
		  if (p1.length == 0 || p2.length == 0) 
		  {
			msg= msg+'Please enter your password. \n';	 
		  }
			
		  if (p1 != p2)  
		  {
			 msg= msg+'The password fields can not be empty. \n';	 
		  } 	
	  } 		
	
	  if(msg=='')
	  {
	  	document.getElementById('form').submit();
		return true;
	  }
	  else
	  {
		alert(msg, 'ERROR');
	 	return false;
	  }
	
	}
</script>